﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Big_Data.View
{
    public partial class FrmReport : Form
    {
        DataBase userDB = new DataBase();
        public FrmReport()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            BindInstallInformationData();
        }

        private void InitDataBind()
        {
            //加油站表
            string S_engineer_sql = "select * from S_area";
            DataTable dt_S_area = userDB.show(S_engineer_sql);
            this.cbbarea.ValueMember = "Area_code";
            this.cbbarea.DisplayMember = "Area_name";
            this.cbbarea.DataSource = dt_S_area;

            //分销商（服务商）表
            string S_distributor_sql = "select * from S_distributor";
            DataTable dt_S_distributor = userDB.show(S_distributor_sql);
            this.cbbdis.ValueMember = "Dis_code";
            this.cbbdis.DisplayMember = "Dis_name";
            this.cbbdis.DataSource = dt_S_distributor;

        }

        private void BindInstallInformationData()
        {
            var area = this.cbbarea.SelectedValue;
            var dis = this.cbbdis.SelectedValue;
            var startdt = this.dtpstartdt.Value;
            var enddt = this.dtpenddt.Value;
            string sqlstr = "select a.Ass_no as '序号',b.Sta_name as '加油站',"
                + " a.Ass_date as '安装时间',a.Ass_corp,a.Ass_check as '检查状态', a.Ass_in,a.Rec_date,c.Eng_name as '工程师',d.Dis_name '分销商' "
                + " from T_assemble a "
                + " left join S_station b on a.Sta_code=b.Sta_code "
                + " left join S_engineer c on a.Eng_code=c.Eng_code "
                + " left join S_distributor d on a.Dis_code=d.Dis_code "
                + " left join S_area e on b.Area_code=e.Area_code "
                + " where 1=1 ";//2
            if (this.cbbarea.SelectedIndex > 0)
            {
                sqlstr += " and a.sta_code = '" + area + "'";
            }
            if (this.cbbdis.SelectedIndex > 0)
            {
                sqlstr += " and a.Dis_code = '" + dis + "'";
            }
            if (startdt != null)
            {
                sqlstr += " and a.Ass_date between '" + startdt.ToString("yyyy-MM-dd 00:00:00") + "' and '" + enddt.ToString("yyyy-MM-dd 23:59:59") + "'";
            }
            sqlstr += " order by Ass_date desc";
            DataTable dataTable = userDB.show(sqlstr);
            this.dataGridView1.DataSource = dataTable;
        }

        private void FrmReport_Load(object sender, EventArgs e)
        {
            this.dtpstartdt.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-01 00:00:00"));
            InitDataBind();
            this.cbbarea.SelectedIndex = -1;
            this.cbbdis.SelectedIndex = -1;
        }
    }
}
